package com.ruoyi.qualityControl.utils;


import cn.hutool.json.JSONObject;
import com.ruoyi.customerService.component.WebSocketServer;
import io.obswebsocket.community.client.OBSRemoteController;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

public class OBSWebSocketClient{
    private WebSocketClient webSocketClient;

//    public static void main(String[] args) {
//        OBSRemoteController controller = OBSRemoteController.builder()
//                .host("localhost")                  // Default host
//                .port(4455)                         // Default port
//                .password("0Gfxdt0wn3nlhsU8")   // Provide your password here
//                .connectionTimeout(3)
//                .build();
//        controller.connect();
//        controller.sendRequest(null,null);
//
//
//    }


    public void connectToOBS(String obsWebSocketUrl) throws URISyntaxException {
        webSocketClient = new WebSocketClient(new URI(obsWebSocketUrl)) {
            @Override
            public void onOpen(ServerHandshake serverHandshake) {
                // 发送认证请求
                JSONObject authJson = new JSONObject();
                authJson.put("request-type", "auth");
                authJson.put("auth", "0Gfxdt0wn3nlhsU8"); // OBS的WebSocket密码，可以在OBS设置中找到
                send(authJson.toString());
                System.out.println("Connected to OBS WebSocket server.");
            }

            @Override
            public void onMessage(String message) {
                System.out.println("Received message from OBS: " + message);
            }

            @Override
            public void onClose(int i, String s, boolean b) {
                System.out.println("Connection to OBS WebSocket server closed.");
            }

            @Override
            public void onError(Exception e) {
                e .printStackTrace();
            }
        };
        webSocketClient.connect();
    }

    public void setScene(String sceneName) {
        // 发送命令，例如开始录制
        JSONObject startRecording = new JSONObject();
        startRecording.put("request-type", "StartRecording");
        webSocketClient.send(startRecording.toString());
    }

    public static void main(String[] args) {
        try {
            // 连接到OBS的WebSocket服务器
            WebSocketClient wsClient = new WebSocketClient(new URI("ws://localhost:4455")) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    System.out.println("Opened Connection");
                }

                @Override
                public void onMessage(String message) {
                    System.out.println("Received message: " + message);
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    System.out.println("Connection closed");
                }

                @Override
                public void onError(Exception ex) {
                    ex.printStackTrace();
                }
            };
            wsClient.connect();

            // 等待连接建立
            Thread.sleep(1000);

            // 发送命令，例如开始录制
            JSONObject startRecording = new JSONObject();
            startRecording.put("request-type", "StartRecording");
            wsClient.send(startRecording.toString());

            // 关闭连接
            wsClient.close();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}